package com.example.jack.community.persenter.me;

import android.os.Handler;
import android.util.Log;

import com.example.jack.community.data.loginbean.RegisterStatus;
import com.example.jack.community.data.loginbean.RegisterVer;
import com.example.jack.community.model.me.Res_phonenumView;
import com.example.jack.community.view.login.util.Md5;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import okhttp3.Call;

/**
 * 重置接口pre层
 * Created by TulinTonJie on 2017/11/24.
 */

public class Res_pho_numPresenterImp implements Res_pho_numPersenter {

    //重置view接口
    private Res_phonenumView res_phonenumView;

    //等待假象
    private Handler handler = new Handler();

    //log
    private static final String TAG = "TonJie";

    //String
    private static String str_reset = null;

    @Override
    public void ResetNum(final String token, final String mphone) {
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                resetpasswords(token, mphone);
            }
        }, 600);
    }


    /**
     * 重置用户的手机号码
     *
     * @param phnenum
     */
    private void resetpasswords(final String token, final String phnenum) {

        String url;
        url = "http://192.168.1.11/change/";
        OkHttpUtils
                .get()
                .url(url)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Log.d(TAG, "onError: " + id);
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        //获取返回的Json数据,格式化成为Java实体类reg
                        final Gson reg_Ver = new Gson();
                        RegisterVer reg = reg_Ver.fromJson(response, RegisterVer.class);
                        Md5 md = new Md5();
                        str_reset = md.md5(reg.getData()).substring(20, 30);
                        Log.d(TAG, "Res_PassPresenterImp:" + "telephone:" + phnenum +  //手机号码
                                "   passcode:" + str_reset  //passcode
                                + "ChangeDataType:3"  //数据保存类型
                                + "token:" + token); //数据保存类型
                        OkHttpUtils
                                .post()
                                .url("http://192.168.1.11/change/")
                                .addParams("ChangeDataType", "3")
                                .addParams("username", "0")
                                .addParams("newpassword", "0")
                                .addParams("oldpassword", "0")
                                .addParams("telephone", phnenum)
                                .addParams("passcode", str_reset)
                                .addParams("token", token)
                                .build()
                                .execute(new StringCallback() {
                                    @Override
                                    public void onError(Call call, Exception e, int id) {

                                    }

                                    @Override
                                    public void onResponse(String response, int id) {
                                        RegisterStatus shopInfo = null;
                                        try {
                                            Log.d(TAG, "onResponse: 进行post请求" + response);
                                            Gson gson = new Gson();
                                            shopInfo = gson.fromJson(response, RegisterStatus.class);
                                            if (shopInfo.getStatus().equals("0")) {
                                                res_phonenumView.loginSuccess("电话号码更改成功");
                                            }
                                            if (shopInfo.getStatus().equals("1")) {
                                                res_phonenumView.loginFail("数据匹配错误");
                                            }
                                            if (shopInfo.getStatus().equals("2")) {
                                                res_phonenumView.loginFail("服务器保存出错");
                                            }
                                        } catch (JsonSyntaxException e) {
                                            e.printStackTrace();
                                            res_phonenumView.loginFail("服务器保存出错");
                                        }
                                    }
                                });
                    }
                });
    }

    //绑定视图
    @Override
    public void attachView(Res_phonenumView v) {
        this.res_phonenumView = v;
    }

    //解除绑定
    @Override
    public void detachView() {
        res_phonenumView = null;
    }
}
